package com.coda.app.service.impl;

import com.coda.app.mapper.ApartmentMapper;
import com.coda.app.mapper.BrowsingHistoryMapper;
import com.coda.app.mapper.GraphMapper;
import com.coda.app.mapper.RoomMapper;
import com.coda.app.service.BrowsingHistoryService;
import com.coda.appVo.graph.GraphVo;
import com.coda.appVo.history.HistoryItemVo;
import com.coda.entity.ApartmentInfo;
import com.coda.entity.BrowsingHistory;
import com.coda.entity.RoomInfo;
import com.coda.result.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class BrowsingHistoryServiceImpl implements BrowsingHistoryService {

    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private GraphMapper graphMapper;
    @Autowired
    private ApartmentMapper apartmentMapper;

    /**
     * 分页查询浏览历史
     * @param current
     * @param size
     * @return
     */
    @Override
    public PageResult getHistory(Integer current, Integer size) {
        //设置分页参数
        PageHelper.startPage(current,size);
        //分页查询信息
        Page<HistoryItemVo> historyItemVoPage = browsingHistoryMapper.getHistory();
        for (HistoryItemVo historyItemVo : historyItemVoPage) {
            Long roomId = historyItemVo.getRoomId();
            //获取房间号
            RoomInfo roomInfo = roomMapper.getRoomInfo(roomId);
            //获取租金
            BigDecimal money = roomMapper.getRent(roomId);
            //获取图片列表
            List<GraphVo> graph = graphMapper.getGraph(roomId, 2);
            //获取公寓名称
            ApartmentInfo apartment = apartmentMapper.getApartment(roomId);

            historyItemVo.setRoomNumber(roomInfo.getRoomNumber());
            historyItemVo.setRent(money);
            historyItemVo.setRoomGraphVoList(graph);
            historyItemVo.setApartmentName(apartment.getName());
            historyItemVo.setProvinceName(apartment.getProvinceName());
            historyItemVo.setCityName(apartment.getCityName());
            historyItemVo.setDistrictName(apartment.getDistrictName());
        }
        PageResult pageResult = new PageResult(historyItemVoPage.getTotal(), historyItemVoPage.getResult());
        return pageResult;
    }

    /**
     * 保存历史浏览记录
     * @param currentId
     * @param id
     */
    @Override
    public void saveHistory(Long currentId, Long id) {
        BrowsingHistory browsing = new BrowsingHistory();
        browsing.setRoomId(id);
        browsing.setUserId(currentId);
        browsing.setBrowseTime(LocalDateTime.now());

        //查询历史浏览记录，如果有执行update，没有insert
        BrowsingHistory browsingHistory = browsingHistoryMapper.getHistoryById(browsing);
        if (browsingHistory == null ){
            browsingHistoryMapper.insert(browsing);
        }else {
            browsingHistory.setBrowseTime(LocalDateTime.now());
            browsingHistoryMapper.update(browsingHistory);
        }
    }
}
